package com.eight.group.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eight.group.constant.MessageConstant;
import com.eight.group.entity.PageResult;
import com.eight.group.entity.QueryPageBean;
import com.eight.group.entity.Result;
import com.eight.group.pojo.Role;
import com.xqx.eight.group.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/role")
public class RoleController{

    @Reference
    private RoleService roleService;

    //分页查询角色管理(t_user)
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            return roleService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L, null);
        }
    }

    //根据角色ID(role_id)查询关联的permissions信息
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,
                    role);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //修改权限
    @RequestMapping("/changePermissions")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result changePermissions(@RequestBody Role role,Integer[] permissionIds){

        try {
            roleService.changePermissions(role,permissionIds);
            return new Result(true, "修改权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改权限失败");
        }
    }

    //添加角色
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result add(@RequestBody Role role,Integer[] permissionIds){
        try {
            roleService.add(role,permissionIds);
            return new Result(true,"添加角色成功" );
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加角色失败");
        }
    }

    //删除角色
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result delete(Integer id){

        try {
            roleService.delete(id);
            return new Result(true, "删除角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除角色失败");
        }
    }

    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findAll(){
        try {
                return new Result(true,"查询角色成功",roleService.findAll());
            } catch (Exception e) {
                return new Result(false, "查询角色失败");
            }
    }

    ///**
    //     * 检查项查询所有
    //     * @return PageResult对象
    //     */
    //    @RequestMapping("/findAll")
    //    @ResponseBody
    //    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    //    public Result findAll(){
    //        try {
    //            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemService.findAll());
    //        } catch (Exception e) {
    //            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    //        }
    //    }

}
